home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Imaging / RealShapes / GXShpe.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  7.0 KB  |  353 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        GXShpe.cpp
  3.  
  4.     Contains:    QDGXShape class, private to ODShape.
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.     
  12.         <13>     8/29/95    jpa        Throw kODErrInvalidPlatformShape instead of
  13.                                     kODErrInvalidValue. [1278284]
  14.         <12>     8/16/95    NP        1274946: ErrorDef.idl problems. Add include
  15.                                     file.
  16.         <11>     5/25/95    jpa        Use new GX headers [1241078, 1253324]
  17.         <10>     3/20/95    jpa        No need to wrap TRY around use of
  18.                                     ODTempPolygon. [1215160]
  19.          <9>     1/12/95    jpa        Removed five-dollar comment, now bug
  20.                                     1209729. [1209502]
  21.          <8>     12/5/94    jpa        gxSolidFill -> gxWindingFill. [1191192]
  22.          <7>    10/24/94    jpa        Fixed IsRectangular to use ODPolygon
  23.                                     properly. Fixed ContainsPoint. [1186370,
  24.          <6>     9/29/94    RA        1189812: Mods for 68K build.
  25.          <5>      9/9/94    jpa        Implemented InitQDRegion. Fixed crasher in
  26.                                     Copy. [1179306] [1181523]
  27.          <4>     8/17/94    jpa        Fixed bug in Copy method that was
  28.                                     accidentally deleting the copied gxShape
  29.          <3>      8/8/94    jpa        Added Outset method [1178690]
  30.          <2>      8/2/94    jpa        Don't dispose NULL GX shapes.
  31.          <1>     6/15/94    jpa        first checked in
  32.  
  33.     In Progress:
  34.         
  35. */
  36.  
  37.  
  38. #ifndef _ALTPOINT_
  39. #include "AltPoint.h"            /* Use C++ savvy ODPoint and ODRect*/
  40. #endif
  41.  
  42. #ifndef _ALTPOLY_
  43. #include "AltPoly.h"
  44. #endif
  45.  
  46. #ifndef _GXSHPE_
  47. #include "GXShpe.h"
  48. #endif
  49.  
  50. #ifndef _RECTSHPE_
  51. #include "RectShpe.h"
  52. #endif
  53.  
  54. #ifndef SOM_ODTransform_xh
  55. #include "Trnsform.xh"
  56. #endif
  57.  
  58. #ifndef _EXCEPT_
  59. #include "Except.h"
  60. #endif
  61.  
  62. #ifndef _ODDEBUG_
  63. #include "ODDebug.h"
  64. #endif
  65.  
  66. #ifndef _UTILERRS_
  67. #include "UtilErrs.h"
  68. #endif
  69.  
  70. #ifndef __GXGRAPHICS__
  71. #include <GXGraphics.h>
  72. #endif
  73.  
  74.  
  75. #pragma segment QDGXShape
  76.  
  77.  
  78. QDGXShape::QDGXShape( ODGeometryMode mode )
  79.     :RealShape(mode),
  80.      fGXShape(kODNULL),
  81.      fBoundsValid(kODFalse)
  82. {
  83. #if ODDebug
  84.     fType = 3;
  85. #endif
  86. }
  87.  
  88.  
  89. QDGXShape::~QDGXShape( )
  90. {
  91.     if( fGXShape )
  92.         GXDisposeShape(fGXShape);
  93. }
  94.  
  95.  
  96. ODSize
  97. QDGXShape::Purge( ODSize bytes )
  98. {
  99.     if( bytes==0 )
  100.         fBoundsValid = kODFalse;        // Zero means just flush cache
  101.     return RealShape::Purge(bytes);
  102. }
  103.  
  104.  
  105. void
  106. QDGXShape::GetBoundingBox( ODRect *bounds )
  107. {
  108.     if( !fBoundsValid ) {
  109.         if( fGXShape ) {
  110.             ClearGXError();
  111.             GXGetShapeBounds(fGXShape,0,(gxRectangle*)&fBounds);
  112.             ThrowIfFirstGXError();
  113.         } else
  114.             fBounds.Clear();
  115.     }
  116.     if( bounds )
  117.         *bounds = fBounds;
  118. }
  119.  
  120.  
  121. RealShape*
  122. QDGXShape::SetRectangle( const ODRect *r )
  123. {
  124.     if( fGXShape )
  125.         GXSetRectangle(fGXShape, (gxRectangle*)r);
  126.     else {
  127.         fGXShape = GXNewRectangle((gxRectangle*)r);
  128.         GXSetShapeFill(fGXShape,gxWindingFill);
  129.     }
  130.     this->Purge(0);
  131.     ThrowIfFirstGXError();
  132.     return this;
  133. }
  134.  
  135.  
  136. void
  137. QDGXShape::CopyPolygon( ODPolygon &poly )
  138. {
  139.     ASSERT_NOT_NULL(fGXShape);
  140.     poly.CopyFrom(fGXShape);
  141. }
  142.  
  143.  
  144. RealShape*
  145. QDGXShape::SetPolygon( const ODPolygon &poly )
  146. {
  147.     ClearGXError();
  148.     if( fGXShape && GXGetShapeType(fGXShape)==gxPolygonType )
  149.         GXSetPolygons(fGXShape, (gxPolygons*)poly.GetData() );
  150.     else {
  151.         gxShape shape = GXNewPolygons( (gxPolygons*)poly.GetData() );
  152.         GXSetShapeFill(shape,gxWindingFill);
  153.         if( shape ) {
  154.             if( fGXShape ) GXDisposeShape(fGXShape);
  155.             fGXShape = shape;
  156.         }
  157.     }
  158.     this->Purge(0);
  159.     ThrowIfFirstGXError();
  160.     return this;
  161. }
  162.  
  163.  
  164. void
  165. QDGXShape::InitQDRegion( )
  166. {
  167.     // Unfortunately in GX 1.0 there is no good way to convert a shape to a region.
  168.     
  169.     ODTempPolygon poly;
  170.     poly.CopyFrom(fGXShape);
  171.     fQDRegion = poly.AsQDRegion();
  172. }
  173.  
  174.  
  175. gxShape
  176. QDGXShape::CopyGXShape( )
  177. {
  178.     ASSERT_NOT_NULL(fGXShape);
  179.     return GXCloneShape(fGXShape);
  180. }
  181.  
  182.  
  183. void
  184. QDGXShape::SetPlatformShape( ODGraphicsSystem system, ODPlatformShape s )
  185. {
  186.     ASSERT(system==kODQuickDrawGX,kODErrAssertionFailed);
  187.  
  188.     ASSERT_NOT_NULL(s);
  189.     if( s != fGXShape ) {
  190.         gxShapeType type = GXGetShapeType((gxShape)s);
  191.         if( type!=gxRectangleType && type!=gxPolygonType && type!=gxPathType
  192.                 && type!=gxEmptyType && type!=gxFullType )
  193.             THROW(kODErrInvalidPlatformShape);
  194.         
  195.         if( fGXShape ) GXDisposeShape(fGXShape);
  196.         fGXShape = (gxShape) s;
  197.         this->Purge(0);
  198.     }
  199. }
  200.  
  201.  
  202. ODBoolean
  203. QDGXShape::IsSameAs( RealShape *shape )
  204. {
  205.     ASSERT_NOT_NULL(fGXShape);
  206.     if( shape==this )
  207.         return kODTrue;
  208.     if( this->IsEmpty() )
  209.         return shape->IsEmpty();
  210.     else if( shape->IsEmpty() )
  211.         return this->IsEmpty();
  212.     else {
  213.         gxShape itsShape = (gxShape) shape->GetPlatformShape(kODQuickDrawGX);
  214.         ClearGXError();
  215.         Boolean same = GXEqualShape(fGXShape,itsShape);
  216.         GXDisposeShape(itsShape);
  217.         ThrowIfFirstGXError();
  218.         return same;
  219.     }
  220. }
  221.  
  222.  
  223. ODBoolean
  224. QDGXShape::IsEmpty( )
  225. {
  226.     ASSERT_NOT_NULL(fGXShape);
  227.     gxShapeType type= GXGetShapeType(fGXShape);
  228.     return (type==gxEmptyType || type==gxPointType || type==gxLineType || type==gxCurveType);
  229. }
  230.  
  231.  
  232. ODBoolean
  233. QDGXShape::ContainsPoint( ODPoint point )
  234. {
  235.     // We have to make the rectangle sliiightly non-empty or GX will ignore it.
  236.     ASSERT_NOT_NULL(fGXShape);
  237.     gxRectangle r = {point.x,point.y, point.x+1,point.y+1};
  238.     return GXTouchesBoundsShape(&r,fGXShape);
  239. }
  240.  
  241.  
  242. ODBoolean
  243. QDGXShape::IsRectangular( )
  244. {
  245.     ASSERT_NOT_NULL(fGXShape);
  246.     gxShapeType type = GXGetShapeType(fGXShape);
  247.     if( type==gxRectangleType )
  248.         return kODTrue;
  249.     
  250.     else if( type==gxPolygonType ) {
  251.         gxShapeAttribute attr = GXGetShapeAttributes(fGXShape);
  252.         GXSetShapeAttributes(fGXShape, attr | gxDirectShape);
  253.         GXLockShape(fGXShape);
  254.         ODSLong size;
  255.         ODPolygon poly;
  256.         poly.SetData( (ODPolygonData*) GXGetShapeStructure(fGXShape,&size) );
  257.         ODBoolean result = poly.IsRectangular();
  258.         GXUnlockShape(fGXShape);
  259.         GXSetShapeAttributes(fGXShape,attr);
  260.         return result;
  261.         
  262.     } else
  263.         return kODFalse;
  264. }
  265.  
  266.  
  267. RealShape*
  268. QDGXShape::Copy( )
  269. {
  270.     ASSERT_NOT_NULL(fGXShape);
  271.     gxShape shape = GXCopyToShape(kODNULL,fGXShape);
  272.     QDGXShape *s = kODNULL; ODVolatile(s);
  273.     TRY{
  274.         s = new QDGXShape(fMode);
  275.         s->SetPlatformShape(kODQuickDrawGX, shape);
  276.     }CATCH_ALL{
  277.         GXDisposeShape(shape);
  278.         delete s;
  279.         RERAISE;
  280.     }ENDTRY
  281.     return s;
  282. }
  283.  
  284.  
  285. RealShape*
  286. QDGXShape::Transform( Environment *ev, ODTransform *xform )
  287. {
  288.     ASSERT_NOT_NULL(fGXShape);
  289.     
  290.     ClearGXError();
  291.     gxMapping map;
  292.     xform->GetMatrix(ev,(ODMatrix*)&map);
  293.     GXMapShape(fGXShape,&map);
  294.     ThrowIfGXError();
  295.     this->Purge(0);
  296.     return this;
  297. }
  298.  
  299.  
  300. RealShape*
  301. QDGXShape::Outset( ODCoordinate distance )
  302. {
  303.     ASSERT_NOT_NULL(fGXShape);
  304.     
  305.     ClearGXError();
  306.     GXInsetShape(fGXShape,-distance);
  307.     ThrowIfGXError();
  308.     this->Purge(0);
  309.     return this;
  310. }
  311.  
  312.  
  313. RealShape*
  314. QDGXShape::Subtract( RealShape *shape )
  315. {
  316.     ASSERT_NOT_NULL(fGXShape);
  317.     ClearGXError();
  318.     gxShape itsShape = (gxShape) shape->GetPlatformShape(kODQuickDrawGX);
  319.     GXDifferenceShape(fGXShape,itsShape);
  320.     GXDisposeShape(itsShape);
  321.     ThrowIfFirstGXError();
  322.     this->Purge(0);
  323.     return this;
  324. }
  325.  
  326.  
  327. RealShape*
  328. QDGXShape::Intersect( RealShape *shape )
  329. {
  330.     ASSERT_NOT_NULL(fGXShape);
  331.     ClearGXError();
  332.     gxShape itsShape = (gxShape) shape->GetPlatformShape(kODQuickDrawGX);
  333.     GXIntersectShape(fGXShape,itsShape);
  334.     GXDisposeShape(itsShape);
  335.     ThrowIfFirstGXError();
  336.     this->Purge(0);
  337.     return this;
  338. }
  339.  
  340.  
  341. RealShape*
  342. QDGXShape::Union( RealShape *shape )
  343. {
  344.     ASSERT_NOT_NULL(fGXShape);
  345.     ClearGXError();
  346.     gxShape itsShape = (gxShape) shape->GetPlatformShape(kODQuickDrawGX);
  347.     GXUnionShape(fGXShape,itsShape);
  348.     GXDisposeShape(itsShape);
  349.     ThrowIfFirstGXError();
  350.     this->Purge(0);
  351.     return this;
  352. }
  353.